﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public partial class frmCertificate : BaseWinFrom
{
    Model.Certificate Certificate;

    public frmCertificate(Model.Certificate certificate)
    {
        InitializeComponent();
        Certificate = certificate ?? new Model.Certificate();
        Certificate.Password = null;

        txtId.Text = Certificate.Id;
        txtName.Text = Certificate.Name;
        if (Certificate.HasPermission(Model.Certificate.Permission.Certificate))
        {
            ckbPermissions1.Checked = true;
        }
        if (Certificate.HasPermission(Model.Certificate.Permission.Diagnosis))
        {
            ckbPermissions2.Checked = true;
        }
        if (Certificate.HasPermission(Model.Certificate.Permission.Drugs))
        {
            ckbPermissions3.Checked = true;
        }
        if (Certificate.HasPermission(Model.Certificate.Permission.UserBase))
        {
            ckbPermissions4.Checked = true;
        }

        Certificate.SetPermission(Model.Certificate.Permission.None);
    }

    private void btOK_Click(object sender, EventArgs e)
    {
        if (txtPassword.Text != txtRepeatPassword.Text)
        {
            MessageBox.Show("密码不一致");
        }
        Certificate.Name = txtName.Text;
        Certificate.Password = txtPassword.Text;
        Certificate.Status = ckbStatus.Checked ? 1 : -1;

        if (ckbPermissions1.Checked == true)
        {
            Certificate.SetPermission(Model.Certificate.Permission.Certificate);
        }
        if (ckbPermissions2.Checked == true)
        {
            Certificate.SetPermission(Model.Certificate.Permission.Diagnosis);
        }
        if (ckbPermissions3.Checked == true)
        {
            Certificate.SetPermission(Model.Certificate.Permission.Drugs);
        }
        if (ckbPermissions4.Checked == true)
        {
            Certificate.SetPermission(Model.Certificate.Permission.UserBase);
        }

        if (new RST.Certificate().Update(Certificate))
        {
            MessageBox.Show("修改成功");
            this.Dispose();
        }
        else
        {
            MessageBox.Show("修改失败");
        }
    }
}